//
//  TicketRecordDetailViewController.m
//  customer
//
//  Created by Holothurian on 2018/11/6.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "TicketRecordDetailViewController.h"
#import "TicketCenterViewTableViewCell.h"

static NSString *SubtitleTableCellID = @"SubtitleTableCellID";
static NSString *DefaultTableCellID = @"DefaultTableCellID";
static NSString *CompanyInfoTableCellID = @"CompanyInfoTableCellID";
@interface TicketRecordDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray *keyArray;

@property (nonatomic,strong)NSMutableArray *valueArray;

@end

@implementation TicketRecordDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"开票详情";
    [self createTicketsView];
    
}


- (void)createTicketsView{
    AutoWeakSelf
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(NAVIGATION_BAR_HEIGHT);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_offset(F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(0));
    }];
    [self.tableView registerClass:[TicketDetailSubtitleTableCell class] forCellReuseIdentifier:SubtitleTableCellID];
    [self.tableView registerClass:[TicketDetailDefaultTableCell class] forCellReuseIdentifier:DefaultTableCellID];
    [self.tableView registerClass:[TicketDetailCompanyInfoTableCell class] forCellReuseIdentifier:CompanyInfoTableCellID];
    [self.keyArray addObject:@[self.ticketModel.invoice_state_show?:@"",@"订单编号"]];
    [self.keyArray addObject:@[@""]];
    [self.keyArray addObject:@[@"收件人",@"联系电话",@"收件地址"]];
    [self.keyArray addObject:@[@"物流名称",@"快递单号"]];
    
    [self.valueArray addObject:@[self.ticketModel.update_time?:@"",self.ticketModel.invoice_no?:@""]];
    [self.valueArray addObject:@[@""]];
    [self.valueArray addObject:@[self.ticketModel.invoice_member?:@"",self.ticketModel.invoice_member_phone?:@"",self.ticketModel.invoice_member_address?:@""]];
    [self.valueArray addObject:@[self.ticketModel.courier_company?:@"",self.ticketModel.courier_number?:@""]];
    if ([self.ticketModel.invoice_state isEqualToString:@"1"]) {//已处理
        UIButton *appointButton = [UIButton buttonWithType:UIButtonTypeCustom];
        appointButton.layer.cornerRadius = 6;
        appointButton.layer.masksToBounds = YES;
        [appointButton setBackgroundColor:[HSPublic colorWithHexString:@"333333"]];
        [appointButton setTitle:@"联系客服" forState:UIControlStateNormal];
        [appointButton setTitle:@"联系客服" forState:UIControlStateSelected];
        [appointButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [appointButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [appointButton addTarget:self action:@selector(contactServiceAction) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:appointButton];
        [appointButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.view).offset(30);
            make.right.equalTo(weakSelf.view).offset(-30);
            make.bottom.equalTo(weakSelf.view).offset(-bottomPadding(20));
            make.height.mas_offset(50);
        }];
        [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.view).offset(NAVIGATION_BAR_HEIGHT);
            make.left.right.equalTo(weakSelf.view);
            make.height.mas_offset(F_DEVICE_H - NAVIGATION_BAR_HEIGHT - bottomPadding(100));
        }];
    }
}


- (void)contactServiceAction{
    [[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:kContactPhone]];
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.keyArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [(NSMutableArray *)self.keyArray[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0&& indexPath.row == 0) {
        TicketDetailSubtitleTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:SubtitleTableCellID];
        cell.nameLabel.text = self.keyArray[indexPath.section][indexPath.row];
        cell.subTitleLabel.text = self.valueArray[indexPath.section][indexPath.row];
        return cell;
    }else if (indexPath.section == 1){
        TicketDetailCompanyInfoTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CompanyInfoTableCellID];
        [cell cellForLayoutCompanyCell:self.ticketModel];
        return cell;
    }else{
        TicketDetailDefaultTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DefaultTableCellID];
        cell.nameLabel.text = self.keyArray[indexPath.section][indexPath.row];
        cell.subTitleLabel.text = self.valueArray[indexPath.section][indexPath.row];
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
       return 225;
    }
    return 50.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, F_DEVICE_W, 30)];
    backView.backgroundColor = [UIColor appColor];
    NSArray *titleArr = @[@"",@"发票信息",@"收件信息",@"物流信息"];
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = titleArr[section];
    titleLabel.textColor = [HSPublic colorWithHexString:@"999999"];
    titleLabel.font = Font(12);
    [backView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView).offset(30);
        make.top.equalTo(backView);
        make.right.equalTo(backView).offset(-30);
        make.height.mas_offset(30);
    }];
    return backView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (NSMutableArray *)keyArray{
    if (!_keyArray) {
        _keyArray = [NSMutableArray array];
    }
    return _keyArray;
}

- (NSMutableArray *)valueArray{
    if (!_valueArray) {
        _valueArray = [NSMutableArray array];
    }
    return _valueArray;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
